package cn.hotel.mapper;

import cn.hotel.pojo.Member;
import cn.hotel.pojo.Member;
import cn.hotel.utlis.StringUtil;
import cn.hotel.utlis.Crud;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MemberDaoImpl extends BaseDao implements MemberDao{
    @Override
    public Member login(String account) {
        String sql ="SELECT ACCOUNT,PASSWORD FROM MEMBER WHERE ACCOUNT=? LIMIT 1";
        List<Object> param = new ArrayList<>();
        param.add(account);
        Member member = null;
        ResultSet resultSet = this.executeQuery(sql,param);
        try {
            while (resultSet.next()){
                member = new Member();
                member.setAccount(resultSet.getString(1));
                member.setPassword(resultSet.getString(2));

            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            this.closeSource(resultSet);
        }
        return member;



    }

    @Override
    public int register(Member member) {
        String sql = "INSERT INTO `MEMBER`(`ID`,`ACCOUNT`,`PASSWORD`,`INTEGRAL`,`DISCOUNT`) VALUES (?,?,?,0,0.8)";
        List<Object> param = new ArrayList<>();
        param.add(member.getId());
        param.add(member.getAccount());
        param.add(member.getPassword());


        return this.executeUpdate(sql,param);
    }

    @Override
    public List<Member> getMemberList(String account, int index, int size) {
        String sql = "SELECT * FROM MEMBER ";
        StringBuffer sbf = new StringBuffer(sql);
        List<Object> param = new ArrayList<>();
        if (StringUtil.isNotEmpty(account)) {
            sbf.append(" WHERE ACCOUNT = ?");
            param.add(account);
        }
        sbf.append(" LIMIT ?,?");
        param.add(index);
        param.add(size);
        ResultSet rs = this.executeQuery(sbf.toString(),param);
        List<Member> memberList = new ArrayList<>();
        Member member = null;
        try {
            while (rs.next()){
                member = new Member();
                member.setId(rs.getLong(1));
                member.setAccount(rs.getString(2));
                member.setPassword(rs.getString(3));
                member.setIntegral(rs.getInt(4));
                member.setDiscount(rs.getDouble(5));


                memberList.add(member);
            }
        }catch (SQLException e) {
            e.printStackTrace();
        }finally {
            this.closeSource(rs);
        }
        return memberList;
    }

    @Override
    public int getCount(String account) {
        String sql = "SELECT COUNT(1) FROM MEMBER ";
        StringBuffer sbf = new StringBuffer(sql);
        List<Object> param = new ArrayList<>();
        if (StringUtil.isNotEmpty(account)) {
            sbf.append(" WHERE ACCOUNT = ?");
            param.add(account);
        }
        int count = 0;
        ResultSet rs = this.executeQuery(sbf.toString(),param);
        try {
            while (rs.next()){
                count = rs.getInt(1);
            }
        }catch (SQLException e) {
            e.printStackTrace();
        }finally {
            this.closeSource(rs);
        }
        return count;

    }

    @Override
    public int deleteMember(String account) {
        String sql = "DELETE FROM MEMBER WHERE ACCOUNT = ?";
        List<Object> param = new ArrayList<>();
        param.add(account);
        int resultSet = this.executeUpdate(sql,param);
        return resultSet;


    }




}
